<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>权限列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
     <el-card class="box-card">
       <el-button type="primary">添加角色</el-button>
       <!-- 表格 -->
      <el-table :data="rolesList" style="width: 100%" stripe border>
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row :class="['bdbottom', t === 0 ? 'bdtop' : '', 'vcenter']" v-for="(item, t) in scope.row.children" :key="item.id">
              <el-col :span="5">
                <el-tag closable @close="removeRightById(scope.row, item.id)">{{item.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="19">
                <el-row :class="[t1 === 0 ? '' : 'bdtop', 'vcenter']" v-for="(item1, t1) in item.children" :key="item1.id">
                  <el-col :span="6">
                    <el-tag closable @close="removeRightById(scope.row, item1.id)" type="success">{{item1.authName}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-tag closable @close="removeRightById(scope.row, item2.id)" type="warning" v-for="(item2) in item1.children" :key="item2.id">{{item2.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
            <!-- <pre>
              {{scope.row}}
            </pre> -->
          </template>
        </el-table-column>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="roleName" label="角色名称"></el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
            <el-button type="warning" icon="el-icon-setting" size="mini" @click="assignPermissions(scope.row)">分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分配权限的对话框 -->
      <el-dialog
  title="提示"
  :visible.sync="permissionsVisible"
  width="50%"
  @close="dialogClosed">
  <el-tree :data="assignList" :props="treeProps" show-checkbox node-key='id' :default-expand-all=true :default-checked-keys='defKeys' ref="treeRef"></el-tree>
  <span slot="footer" class="dialog-footer">
    <el-button @click="permissionsVisible = false">取 消</el-button>
    <el-button type="primary" @click="allotRights">确 定</el-button>
  </span>
</el-dialog>
     </el-card>
  </div>
</template>

<script>
import { rolesList, deleteRolsePre, getAssignList, authorizationRole } from './service'
export default {
  data() {
    return {
      rolesList: [],
      permissionsVisible: false, // 分配权限对话框
      assignList: [], // 权限tree
      treeProps: {
        label: 'authName',
        children: 'children'
      }, // 树形控件的绑定对象
      defKeys: [],
      roleId: ''
    }
  },
  created() {
    this.getRolesList()
  },
  methods: {
    getRolesList() {
      rolesList().then(res => {
        console.log(res.data)
        if (res.data.meta.status !== 200) {
          return this.message.error('获取角色列表失败')
        }
        this.rolesList = res.data.data
        this.$message.success('获取角色列表成功')
      })
    },
    async removeRightById(role, rightId) {
      const confirm = await this.$confirm('删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirm !== 'confirm') return this.$message.info('取消了删除')
      deleteRolsePre(role.id, rightId).then(res => {
        if (res.data.meta.status !== 200) {
          return this.$message.error('删除角色权限失败')
        }
        role.children = res.data.data
        this.$message.success('删除角色权限成功')
      })
    },
    // 分配权限
    assignPermissions(role) {
      console.log(role)
      this.roleId = role.id
      getAssignList('tree').then(res => {
        console.log(res)
        if (res.data.meta.status !== 200) {
          return this.$message.error('获取权限列表失败')
        }
        this.$message.success('获取权限列表成功')
        this.assignList = res.data.data
        console.log(this.assignList)
      })
      this.getLeafKeys(role, this.defKeys)
      this.permissionsVisible = true
    },
    // 通过递归的形式获取角色下三级权限的id 并保存到数组中
    getLeafKeys(node, arr) {
      if (!node.children) {
        return arr.push(node.id)
      }
      node.children.forEach(item => {
        this.getLeafKeys(item, arr)
      })
    },
    dialogClosed() {
      this.defKeys = []
    },
    allotRights() {
      const keys = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys()
      ]
      console.log(keys)
      const idStr = keys.join(',')
      authorizationRole(this.roleId, idStr).then(res => {
        console.log(res)
        if (res.data.meta.status !== 200) {
          return this.$message.error('分配角色失败')
        }
        this.$message.success('分配角色成功')
        this.getRolesList()
        this.permissionsVisible = false
      })
    }
  }
}
</script>

<style lang="less" scoped>
.el-tag {
  margin: 7px;
}
.bdtop {
  border-top: 1px solid #eee;
}
.bdbottom {
  border-bottom: 1px solid #eee;
}
.vcenter {
  display: flex;
  align-items: center;
}
</style>
